<template>
 <van-button
    v-if="!isFollowed"
    class="follow-btn"
    type="info"
    color="#3296fa"
    round
    size="small"
    icon="plus"
    @click="clickFollowed"
    :loading="isFollowedLoading"
    >关注</van-button>
    <van-button
    v-else
    class="follow-btn"
    round
    size="small"
    @click="clickFollowed"
    :loading="isFollowedLoading"
    >已关注</van-button>
</template>

<script>
import { followedUser, cancelFollowedUser } from '@/api/user'
export default {
  name: 'followed-user',
  data () {
    return {
      isFollowedLoading: false
    }
  },
  props: {
    isFollowed: {
      type: Boolean,
      required: true
    },
    autId: {
      type: [Number, Object],
      required: true
    }
  },
  // 修改v-model默认传递的属性和默认定义的事件名称
  model: {
    // 修改默认的属性value
    prop: 'isFollowed',
    // 修改默认的事件名称input
    event: 'update-follow'
  },
  created () {

  },

  methods: {
    // 点击关注或取消关注
    async clickFollowed () {
      this.isFollowedLoading = true
      try {
        if (this.isFollowed) {
          // 已关注 操作取消关注
          await cancelFollowedUser(this.autId)
        } else {
          // 未关注 操作关注
          await followedUser(this.autId)
        }
      } catch (err) {
        if (err.response && err.response.status === 400) {
          this.isFollowedLoading = false
          return this.$toast.fail('亲不能关注自己')
        }
        this.$toast.fail('操作失败')
      }
      this.isFollowedLoading = false
      // this.article.is_followed = !this.article.is_followed
      this.$emit('update-follow', !this.isFollowed)
    }
  }
}
</script>

<style scoped lang='less'>

</style>
